package com.laizhenghua.hotel.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.laizhenghua.hotel.service.*;
import com.laizhenghua.hotel.utils.PageUtils;
import com.laizhenghua.hotel.utils.R;
import com.laizhenghua.hotel.vo.OrderVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.laizhenghua.hotel.entity.ApplyEntity;

/**
 * 
 *
 * @author laizhenghua
 * @email 3299447929@qq.com
 * @date 2021-03-11 15:23:22
 */
@Controller
@RequestMapping("hotel/apply")
public class ApplyController {
    @Autowired
    private ApplyService applyService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private CatesService catesService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private AdsService adsService;
    @Autowired
    private NewsService newsService;

    /**
     * 获取订单页面数据
     */
    @GetMapping("/apply/get_order_list")
    @ResponseBody
    public R getOrderListData(){
        List<ApplyEntity> list = applyService.list();
        List<OrderVo> orderVoList = list.stream().map(item -> {
            OrderVo orderVo = new OrderVo();
            BeanUtils.copyProperties(item, orderVo);
            orderVo.setHouseEntity(houseService.getById(item.getHid()));
            orderVo.setUsersEntity(usersService.getById(item.getUserid()));
            return orderVo;
        }).collect(Collectors.toList());
        return R.ok().put("list",orderVoList);
    }
    /**
     * 后台跳转至订单管理页面
     */
    @GetMapping("/apply/list")
    public String toOrderListPage(){
        return "admin/apply/list";
    }
    /**
     * 后台首页数据页面跳转
     */
    @GetMapping("/report")
    public String toReportPage(){
        return "admin/apply/report";
    }
    /**
     * 后台首页数据获取
     * API: http://localhost:8080/hotel/apply/report/data
     */
    @GetMapping("/report/data")
    @ResponseBody
    public R getReportData() {
        Map<String,Object> map = new HashMap<>();
        map.put("n1",usersService.list().size());
        map.put("n2",catesService.list().size());
        map.put("n3",houseService.list().size());
        map.put("n4",applyService.list().size());
        map.put("n5",adsService.list().size());
        map.put("n6",newsService.list().size());
        return R.ok().put("data",map);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = applyService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    public R info(@PathVariable("id") Integer id){
		ApplyEntity apply = applyService.getById(id);

        return R.ok().put("apply", apply);
    }
    @RequestMapping("/info/by_username")
    @ResponseBody
    public R getInfoByTelAndUsername(@RequestParam("tel") String tel,@RequestParam("username")String username) {
        // System.out.println(tel+username);
        ApplyEntity applyEntity = applyService.getOneByTelAndUsername(tel,username);
        if(applyEntity != null){
            return R.ok().put("data",applyEntity);
        }
        return R.error().put("data","没有此数据");
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ResponseBody
    public R save(@RequestBody ApplyEntity apply){
		applyService.save(apply);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ResponseBody
    public R update(@RequestBody ApplyEntity apply){
		applyService.updateById(apply);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    public R delete(@RequestBody Integer[] ids){
		applyService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
